<?php
// 指定允许其他域名访问  
header('Access-Control-Allow-Origin:*');
// 响应类型  
header('Access-Control-Allow-Methods:POST');
// 响应头设置  
header('Access-Control-Allow-Headers:x-requested-with,content-type:application/json,charset:utf-8');
require_once 'PHPExcel-1.8\Classes\PHPExcel.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\IOFactory.php';
// require_once 'PHPExcel\Classes\PHPExcel\Reader\Excel5.php';
require_once 'PHPExcel-1.8\Classes\PHPExcel\Reader\Excel2007.php';
class Dorm extends CI_Controller{

    public function __construct(){

        parent::__construct();
        $this->load->model('Dorm/Dorm_manage');
        $this->load->helper('url_helper');
        $this->load->library('session');
        // $this->load->helper('date');
        $this->load->helper(array('form', 'url', 'date'));
    }

    //宿舍楼新增方法
    public function dorm_insert(){

        $area_number = $_POST['area_number'];       //区域编号
        $floor_name = $_POST['floor_name'];         //宿舍楼名
        $floorNumber = $_POST['floorNumber'];       //宿舍楼层数
        $floor_cname = $_POST['floor_cname'];       //宿舍楼创建人
        $remarks = $_POST['remarks'];               //宿舍楼备注
        $adminName = $_POST['adminName'];           //宿舍楼管理员
        $liveNumber = $_POST['liveNumber'];         //宿舍楼人数
        $floor_location = $_POST['floor_location']; //宿舍楼位置

        // $area_number = "1585";
        // $floor_name = "测试";
        // $floorNumber = "6";
        // $floor_cname = "陈羿华";
        // $remarks = "测试";
        // $adminName = "陈羿华";
        // $liveNumber = "100";
        // $floor_location = "慈溪";

        $result_json = $this->Dorm_manage->dorm_insert(
            $area_number,
            $floor_name,
            $floorNumber,
            $floor_cname,
            $remarks,
            $adminName,
            $liveNumber,
            $floor_location
        );

        echo $result_json;

        return 0;
    }

    //宿舍楼修改方法
    public function dorm_update(){
        
        $area_name = $_POST['area_name'];           //区域名称
        $area_number = $_POST['area_number'];       //区域编号
        $floor_name = $_POST['floor_name'];         //宿舍楼名称
        $floor_number = $_POST['floor_number'];     //宿舍楼编号
        $floor_location = $_POST['floor_location']; //宿舍楼位置名称
        $floorNumber = $_POST['floorNumber'];       //宿舍楼层数
        $floor_cname = $_POST['floor_cname'];       //宿舍楼创建人
        $floor_cdate = $_POST['floor_cdate'];       //宿舍楼创建日期
        $remarks = $_POST['remarks'];               //宿舍楼备注
        $adminName = $_POST['adminName'];           //宿舍楼管理员
        $liveNumber = $_POST['liveNumber'];         //宿舍楼人数

        // $area_name = "测试";
        // $area_number = "178";
        // $floor_name = "周塘西街178号";
        // $floor_number = "4";
        // $floor_location = "周塘西街178号";
        // $floorNumber = "1";
        // $floor_cname = "陈羿华";
        // $floor_cdate = "2020-10-27";
        // $remarks = "备注";
        // $adminName = "杨静";
        // $liveNumber = "100";

        $result_json = $this->Dorm_manage->dorm_update(
            $area_name,
            $area_number,
            $floor_name,
            $floor_number,
            $floor_location,
            $floorNumber,
            $floor_cname,
            $floor_cdate,
            $remarks,
            $adminName,
            $liveNumber
        );

        echo $result_json;

        return 0;
    }

    //宿舍楼删除方法
    public function dorm_delete(){

        $area_number = $_POST['area_number'];   //区域编号
        $floor_number = $_POST['floor_number']; //宿舍楼编号

        // $area_number = "1585";
        // $floor_number = "5";

        $result_json = $this->Dorm_manage->dorm_delete(
            $area_number,
            $floor_number
        );

        echo $result_json;

        return 0;
    }

    // 宿舍楼信息管理搜索方法
    public function dorm_select(){

        $area_number = $_POST['area_number'];   //区域编号
        $floor_number = $_POST['floor_number']; //宿舍楼编号
        $adminName = $_POST['adminName'];       //宿舍楼管理员
        $limit = $_POST['page'];                //页数

        // $area_number = "";
        // $floor_number = "";
        // $adminName = "";
        // $limit = ""; 
    
        $result_json = $this->Dorm_manage->dorm_select(
            $area_number,
            $floor_number,
            $adminName,
            $limit
        );
    
            echo $result_json;
    
            return 0;
        }

    //人员信息导出excel
    public function excel_dorm(){        
        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  
        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','区域名称');
        $objActSheet->setCellValue('B1','宿舍楼名称');
        $objActSheet->setCellValue('C1','宿舍楼层号');
        $objActSheet->setCellValue('D1','宿舍房间号');
        $objActSheet->setCellValue('E1','姓名');
        $objActSheet->setCellValue('F1','性别');
        $objActSheet->setCellValue('G1','工号');
        $objActSheet->setCellValue('H1','身份证号');
        $objActSheet->setCellValue('I1','人员编码');
        $objActSheet->setCellValue('J1','部门');
        $objActSheet->setCellValue('K1','岗位');
        $objActSheet->setCellValue('L1','籍贯');
        $objActSheet->setCellValue('M1','户籍地址');
        $objActSheet->setCellValue('N1','职级'); 
        $objActSheet->setCellValue('O1','班次'); 
        $objActSheet->setCellValue('P1','联系方式'); 
        $objActSheet->setCellValue('Q1','紧急联系人'); 
        $objActSheet->setCellValue('R1','与紧急联系人关系'); 
        $objActSheet->setCellValue('S1','紧急联系人联系方式'); 
        $objActSheet->setCellValue('T1','是否已入住'); 
        $objActSheet->setCellValue('U1','入住日期'); 
        $objActSheet->setCellValue('V1','是否已退宿'); 
        $objActSheet->setCellValue('W1','退宿类型'); 
        $objActSheet->setCellValue('X1','退宿日期'); 
        $objActSheet->setCellValue('Y1','是否重复入住'); 
        $objActSheet->setCellValue('Z1','是否入黑名单'); 
        $objActSheet->setCellValue('AA1','床位号'); 
        $objActSheet->setCellValue('AB1','柜子号'); 
        $objActSheet->setCellValue('AC1','是否免水电'); 
        $objActSheet->setCellValue('AD1','免水电天数'); 
        $objActSheet->setCellValue('AE1','备注信息'); 
        $objActSheet->setCellValue('AF1','是否临时'); 
        $num = 2;

        $data = $this->Dorm_manage->excel_dorm();
        $data = json_decode($data,true);

        for ($i=0; $i < count($data['msg']); $i++) {
            //性别进行替换
            if($data['msg'][$i]['sex'] == 0){
                $data['msg'][$i]['sex'] = '女';
            } else {
                $data['msg'][$i]['sex'] = '男';
            }

            //是否入住进行替换
            if($data['msg'][$i]['liveCheck'] == 'X'){
                $data['msg'][$i]['liveCheck'] = '已入住';
            } else {
                $data['msg'][$i]['liveCheck'] = '未入住';
            }
            
            //是否退宿进行替换
            if($data['msg'][$i]['quitCheck'] == 'X'){
                $data['msg'][$i]['quitCheck'] = '已退宿';
            } else {
                $data['msg'][$i]['quitCheck'] = '未退宿';
            }
        }

        for ($i=0; $i < count($data['msg']); $i++) 
        { 
            $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['area_name'] , PHPExcel_Cell_DataType::TYPE_STRING);                 //区域名称
            $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['floor_name'] , PHPExcel_Cell_DataType::TYPE_STRING);                //宿舍楼名称
            $objActSheet->setCellValueExplicit('C' . $num, $data['msg'][$i]['dorm_floor'] , PHPExcel_Cell_DataType::TYPE_STRING);               //宿舍楼层号
            $objActSheet->setCellValueExplicit('D' . $num,$data['msg'][$i]['room_number'] , PHPExcel_Cell_DataType::TYPE_STRING);               //宿舍房间号 
            $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['name'], PHPExcel_Cell_DataType::TYPE_STRING);                       //姓名
            $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['sex'], PHPExcel_Cell_DataType::TYPE_STRING);                        //性别
            $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['jobNumber'], PHPExcel_Cell_DataType::TYPE_STRING);                  //工号
            $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['idNumber'], PHPExcel_Cell_DataType::TYPE_STRING);                   //身份证
            $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['degsonid'], PHPExcel_Cell_DataType::TYPE_STRING);                   //人员编码
            $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['department'], PHPExcel_Cell_DataType::TYPE_STRING);                 //部门
            $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['job'], PHPExcel_Cell_DataType::TYPE_STRING);                        //岗位
            $objActSheet->setCellValueExplicit('L' . $num,$data['msg'][$i]['location'], PHPExcel_Cell_DataType::TYPE_STRING);                   //籍贯
            $objActSheet->setCellValueExplicit('M' . $num,$data['msg'][$i]['address'], PHPExcel_Cell_DataType::TYPE_STRING);                    //户籍地址
            $objActSheet->setCellValueExplicit('N' . $num,$data['msg'][$i]['level'], PHPExcel_Cell_DataType::TYPE_STRING);                      //职级
            $objActSheet->setCellValueExplicit('O' . $num,$data['msg'][$i]['classes'], PHPExcel_Cell_DataType::TYPE_STRING);                    //班次
            $objActSheet->setCellValueExplicit('P' . $num,$data['msg'][$i]['phone'], PHPExcel_Cell_DataType::TYPE_STRING);                      //联系方式
            $objActSheet->setCellValueExplicit('Q' . $num,$data['msg'][$i]['emergencyContact'], PHPExcel_Cell_DataType::TYPE_STRING);           //紧急联系人
            $objActSheet->setCellValueExplicit('R' . $num,$data['msg'][$i]['relationShip'], PHPExcel_Cell_DataType::TYPE_STRING);               //与紧急联系人关系
            $objActSheet->setCellValueExplicit('S' . $num,$data['msg'][$i]['emergencyPhone'], PHPExcel_Cell_DataType::TYPE_STRING);             //紧急联系人联系方式
            $objActSheet->setCellValueExplicit('T' . $num,$data['msg'][$i]['liveCheck'], PHPExcel_Cell_DataType::TYPE_STRING);                  //是否已入住
            $objActSheet->setCellValueExplicit('U' . $num,$data['msg'][$i]['liveDate'], PHPExcel_Cell_DataType::TYPE_STRING);                   //入住日期
            $objActSheet->setCellValueExplicit('V' . $num,$data['msg'][$i]['quitCheck'], PHPExcel_Cell_DataType::TYPE_STRING);                  //是否已退宿
            $objActSheet->setCellValueExplicit('W' . $num,$data['msg'][$i]['retreatType'], PHPExcel_Cell_DataType::TYPE_STRING);                //退宿类型
            $objActSheet->setCellValueExplicit('X' . $num,$data['msg'][$i]['retreatDate'], PHPExcel_Cell_DataType::TYPE_STRING);                //退宿日期
            $objActSheet->setCellValueExplicit('Y' . $num,$data['msg'][$i]['repetitionCheck'], PHPExcel_Cell_DataType::TYPE_STRING);            //是否重复入住
            $objActSheet->setCellValueExplicit('Z' .  $num,$data['msg'][$i]['blacklistCheck'], PHPExcel_Cell_DataType::TYPE_STRING);            //是否入黑名单
            $objActSheet->setCellValueExplicit('AA' . $num,$data['msg'][$i]['bedNumber'], PHPExcel_Cell_DataType::TYPE_STRING);                 //床位号
            $objActSheet->setCellValueExplicit('AB' . $num,$data['msg'][$i]['wardrobe'], PHPExcel_Cell_DataType::TYPE_STRING);                  //柜子号
            $objActSheet->setCellValueExplicit('AC' . $num,$data['msg'][$i]['freeCheck'], PHPExcel_Cell_DataType::TYPE_STRING);                 //是否免水电
            $objActSheet->setCellValueExplicit('AD' . $num,$data['msg'][$i]['freeDays'], PHPExcel_Cell_DataType::TYPE_STRING);                  //免水电天数
            $objActSheet->setCellValueExplicit('AE' . $num,$data['msg'][$i]['temporary'], PHPExcel_Cell_DataType::TYPE_STRING);                 //是否临时
            $objActSheet->setCellValueExplicit('AF' . $num,$data['msg'][$i]['remarks'], PHPExcel_Cell_DataType::TYPE_STRING);                   //备注信息


            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }
        //设置列的属性  
        $objActSheet->getColumnDimension("A")->setWidth('20px');  // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth('20px');  // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth('20px');  // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth('20px');  // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth('20px');  // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("G")->setWidth('20px');  // 设置宽度
        $objActSheet->getColumnDimension("H")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("I")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("J")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("K")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("L")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("M")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("N")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("O")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("P")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("Q")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("R")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("S")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("T")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("U")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("V")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("W")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("X")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("Y")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("Z")->setWidth('20px');  // 设置宽度 
        $objActSheet->getColumnDimension("AA")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("AB")->setWidth('20px'); // 设置宽度
        $objActSheet->getColumnDimension("AC")->setWidth('20px'); // 设置宽度
        $objActSheet->getColumnDimension("AD")->setWidth('20px'); // 设置宽度
        $objActSheet->getColumnDimension("AE")->setWidth('20px'); // 设置宽度
        $objActSheet->getColumnDimension("AF")->setWidth('20px'); // 设置宽度
        
        $outputFileName = iconv('UTF-8', 'gb2312', '宿舍人员信息.xls');  
        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary');  
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
        
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

        $objExcel = null;
    }

    //宿舍楼信息excel导出
    public function excel_dorm_information(){

        // 创建一个处理对象实例   
        $objExcel = new \PHPExcel();  
        // 创建文件格式写入对象实例, uncomment   
        $objProps = $objExcel->getProperties();  
        //设置创建者  TODO:
        $objProps->setCreator('admin');
        //描述  
        // $objProps->setDescription("入住信息汇总");  
        $objActSheet = $objExcel->getActiveSheet();
        //单元格赋值    
        $objActSheet->setCellValue('A1','区域名称');
        $objActSheet->setCellValue('B1','区域编号');
        $objActSheet->setCellValue('C1','宿舍楼编号');
        $objActSheet->setCellValue('D1','宿舍楼名称');
        $objActSheet->setCellValue('E1','地理位置');
        $objActSheet->setCellValue('F1','创建人员');
        $objActSheet->setCellValue('G1','创建时间');
        $objActSheet->setCellValue('H1','楼层数');
        $objActSheet->setCellValue('I1','宿管员');
        $objActSheet->setCellValue('J1','入住人数');
        $objActSheet->setCellValue('K1','备注');
    
        $num = 2;
    
        $data = $this->Dorm_manage->excel_dorm_information();
        $data = json_decode($data,true);
    
        for ($i=0; $i < count($data['msg']); $i++) 
        { 
            $objActSheet->setCellValueExplicit('A' . $num,$data['msg'][$i]['area_name'] , PHPExcel_Cell_DataType::TYPE_STRING);         //区域名称
            $objActSheet->setCellValueExplicit('B' . $num,$data['msg'][$i]['area_number'] , PHPExcel_Cell_DataType::TYPE_STRING);       //区域编号
            $objActSheet->setCellValueExplicit('C' . $num, $data['msg'][$i]['floor_number'] , PHPExcel_Cell_DataType::TYPE_STRING);     //宿舍楼编号
            $objActSheet->setCellValueExplicit('D' . $num, $data['msg'][$i]['floor_name'] , PHPExcel_Cell_DataType::TYPE_STRING);       //宿舍楼名称
            $objActSheet->setCellValueExplicit('E' . $num,$data['msg'][$i]['floor_location'] , PHPExcel_Cell_DataType::TYPE_STRING);    //宿舍楼地址 
            $objActSheet->setCellValueExplicit('F' . $num,$data['msg'][$i]['floor_cname'] , PHPExcel_Cell_DataType::TYPE_STRING);       //创建人
            $objActSheet->setCellValueExplicit('G' . $num,$data['msg'][$i]['floor_cdate'] , PHPExcel_Cell_DataType::TYPE_STRING);       //创建时间
            $objActSheet->setCellValueExplicit('H' . $num,$data['msg'][$i]['floorNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);       //楼层数
            $objActSheet->setCellValueExplicit('I' . $num,$data['msg'][$i]['adminName'] , PHPExcel_Cell_DataType::TYPE_STRING);         //宿管员
            $objActSheet->setCellValueExplicit('J' . $num,$data['msg'][$i]['liveNumber'] , PHPExcel_Cell_DataType::TYPE_STRING);        //入住人数
            $objActSheet->setCellValueExplicit('K' . $num,$data['msg'][$i]['remarks'] , PHPExcel_Cell_DataType::TYPE_STRING);           //备注
            //设置行的属性  
            $objActSheet->getRowDimension($num)->setRowHeight('20px');//设置行高  
            $num = $num + 1;
        }
    
        //设置列的属性  
        $objActSheet->getColumnDimension("A")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("B")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("C")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("D")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("E")->setWidth('20px'); // 设置宽度  
        $objActSheet->getColumnDimension("F")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("G")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("H")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("I")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("J")->setWidth('20px'); // 设置宽度 
        $objActSheet->getColumnDimension("K")->setWidth('20px'); // 设置宽度 
        $outputFileName = iconv('UTF-8', 'gb2312', '宿舍楼信息.xls');  
        //文件直接输出到浏览器  
        header('Pragma:public');  
        header('Expires:0');  
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');  
        header('Content-Type:application/force-download');  
        header('Content-Type:application/vnd.ms-excel');  
        header('Content-Type:application/octet-stream');  
        header('Content-Type:application/download');  
        header('Content-Disposition:attachment;filename=' . $outputFileName);  
        header('Content-Transfer-Encoding:binary');  
        PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007')->save('php://output');
            
        //设置对齐方式      
        $objStyleA3 = $objActSheet->getStyle('A3');  
        $objAlignA3 = $objStyleA3->getAlignment();  
        $objAlignA3->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);  
        $objAlignA3->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
    
        $objExcel = null;
    }
}
